package com.cxl.live.system.service.impl;

import com.cxl.live.base.ResultModel;
import com.cxl.live.exception.BusinessException;
import com.cxl.live.system.dto.Register;
import com.cxl.live.system.dto.User;
import com.cxl.live.system.dto.UserInfo;
import com.cxl.live.system.mapper.RegisterMapper;
import com.cxl.live.system.mapper.UserInfoMapper;
import com.cxl.live.system.mapper.UserMapper;
import com.cxl.live.system.service.RegisterService;
import com.cxl.live.util.RegexUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: RegisterServiceImpl
 * @Author: ChenXueLi
 * @Date: 2019-02-05 16:20
 * 你是一段成熟的代码，要学会自己改bug
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    RegisterMapper registerMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    /**
     * 注册账号
     * @param resultModel 返回对象
     * @param register    注册对象
     * @return
     */
    @Override
    public void register(ResultModel resultModel, Register register) {
        // 初始化对象的基本信息
        register.initModel();
        // 查询当前ip注册12小时内注册的用户数量
        int count = registerMapper.getRegisterCountAfterTime(new Date(register.getCreateTime().getTime() - (60 * 60 * 1000 * 12)), resultModel.getIp());
        if(count >= 5) {
            throw new BusinessException(-1001, "当前网络注册用户过多，请更换网络");
        }
        // 查询注册信息表是否存在相同的用户名（ip不同，用户名相同）
        Register registerGet = registerMapper.getRegisterByAccount(register.getAccount());
        if(registerGet != null) {
            if(registerGet.getIp().equals(resultModel.getIp())) {
                // ip相同，可视同一个用户的
            }
            throw new BusinessException(-1002, "用户名被占用");
        }
        // 查询用户表是否存在相同的用户名
        User user = userMapper.getUserByAccount(register.getAccount());
        if(user != null) {
            throw new BusinessException(-1003, "用户名被占用");
        }
        // 对象设值
        String code = RegexUtil.randomString(4);
        register.setCode(code);
        register.setIsActive(0);
        register.setIp(resultModel.getIp());
        // 设置有效时间期限
        register.setEffectiveTime(new Date(register.getCreateTime().getTime() + (30 * 60 * 1000)));

        // 插入数据
        int effectRows = registerMapper.insert(register);

        // 验证发送方向



        // 设置验证码信息
        Map<String, Object> map = new HashMap<>();
        map.put("code", code);
        map.put("uuid", register.getRegisterId());
        resultModel.setData(map);
        resultModel.info("注册成功，请前往激活账号");
    }

    /**
     * 激活账号
     * @param resultModel 返回对象
     * @param code        验证码
     * @param uuid        账号标识码
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void active(ResultModel resultModel, String code, String uuid) {
        // 将uuid解密成id
        String id = uuid;
        // 获取注册对象
        Register register = registerMapper.getRegisterById(id);
        if(register == null) {
            throw new BusinessException(-1001, "注册信息错误，请重新注册");
        }
        // 验证ip是否相同
        if(resultModel.getIp().equals(register.getIp())) {
            throw new BusinessException(-1002, "请使用不要切换网络激活账号信息");
        }
        if(register.getIsActive() == 1) {
            throw new BusinessException(-1003, "账号已经被激活");
        }

        register.setIsActive(1);
        register.setModifyTime(new Date());

        // 修改注册信息
        int effectRows = registerMapper.activeRegister(register);

        // 将账号添加到用户表中
        User user = new User();
        effectRows = userMapper.insert(user);

        // 将用户信息添加到用户信息表中
        UserInfo userInfo = new UserInfo();
        effectRows = userInfoMapper.insert(userInfo);


    }
}
